package com.pzr.soccer.service;

import com.pzr.soccer.consts.LoginConstant;
import com.pzr.soccer.user.SecurityUser;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

//@Service
public class CustomUserDetailService implements UserDetailsService {
   @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String loginType = attributes.getRequest().getParameter("login_type");
        if (StringUtil.isNullOrEmpty(loginType)) {
            throw new AuthenticationServiceException("请添加登录参数");
        }
        SecurityUser user = null;
        switch (loginType) {
            case LoginConstant
                    .ADMIN_TYPE:
                user = loadAdminUserByUsername(username);
                break;
            case LoginConstant.MEMBER_TYPE:
                user = loadMemberUserByUsername(username);
                break;
            default:
                throw new AuthenticationServiceException("暂不支持的登录方式：" + loginType
                );
        }
        user.setLoginType(loginType);
        return user;
    }

    private SecurityUser loadMemberUserByUsername(String username) {

        return null;
    }

    private List<String> getUserPermissions(long id) {
        String code = jdbcTemplate.queryForObject(LoginConstant.QUERY_ROLE_CODE_SQL, String.class, id);
        if(code==null){
            return  Collections.emptyList();
        }
        List<String> permissions=null;
        if(code.equals(LoginConstant.ADMIN_CODE)){
            permissions=jdbcTemplate.queryForList(LoginConstant.QUERY_ALL_PERMISSIONS,String.class);
        }else {

            permissions=jdbcTemplate.queryForList(LoginConstant.QUERY_PERMISSION_SQL,String.class,id);
        }
        return  permissions;
    }

    private SecurityUser loadAdminUserByUsername(String username) {

        return jdbcTemplate.queryForObject(LoginConstant.QUERY_ADMIN_SQL, new RowMapper<SecurityUser>() {
            @Override
            public SecurityUser mapRow(ResultSet resultSet, int i) throws SQLException {
                if(resultSet.wasNull()){
                    throw new UsernameNotFoundException("用户不存在！");
                }
                long id=resultSet.getLong("id");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                int status = resultSet.getInt("status");
                SecurityUser user=new SecurityUser();
                user.setId(id);
                user.setUsername(username);
                user.setPassword(password);
                user.setAuthorities(getUserPermissions(id));
                return user;
            }
        },username);
    }
}
